<?php
Raise::load('control.RaiseFormField');

/**
 * RaiseFileUploadField UIControl class
 * A text field UI control
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.Core.UI.Form
 * @since 1.2
 */
class RaiseFileUploadField extends RaiseFormField {

    private $maxSize;

    /**
     * Create new RaiseTextField
     */
    public function __construct($name) {
        $this->tag = new RaiseHtmlTag('input', '', array('type' => 'file'));
        $this->clientId($name);
    }

    /**
     * Get or set the maximum size of this file upload
     * @param integer $m (optional) If set, the new maximum size will be set to $m
     * @return integer
     */
    public function maxSize($m = null){
        if(func_num_args() == 1){
            if($m > ini_get('post_max_size')){
                throw new RaiseInvalidArgumentException('maxSize cannot be more than post_max_size as defined in php.ini');
            }
            $this->maxSize = $m;
        }
        return $this->maxSize;
    }
    
    public function output() {
        $max = '';
        if($this->maxSize){
            $maxField = new RaiseHiddenField();
            $maxField->value($this->maxSize);
            $maxField->clientId('MAX_FILE_SIZE');
            $max = $maxField->output();
        }
        if($this->clientId()){
            $this->tag->attributes()->add('id', $this->clientId());
            $this->tag->attributes()->add('name', $this->clientId());
        }
        if($this->cssClass()){
            $this->tag->attributes()->add('class', $this->cssClass());
        }
        return $max . $this->tag->html();
    }

}

